<?php

$strings = array  (
	'history' => 'My Progress Graph:',
	'wrong_answers' => 'Wrong Answers',
	'skip' => 'skip',
	'show_answer' => 'I give up',
	'check_answer' => 'check answer',
	'continue' => 'next question',
	'history_overview_singular' => 'NUM_QUESTIONS question completed, NUM_FIRSTTRY correct on the first try',
	'history_overview_plural' => 'NUM_QUESTIONS questions completed, NUM_FIRSTTRY correct on the first try'
	);

function gi($name) { // gi = get input
	$v = trim($_POST[$name]);
	if (!$v) { return trim($_GET[$name]); }
	return $v;
	}

$config = array (
	'case_sensitive' => false,
	'plural_sensitive' => false,
	'skip' => gi('useskip'),
	'multiplechoice' => gi('multiplechoice')
	);

require_once('get_definitions.php');
$defs = get_definitions();

//chooses a new question to ask
require_once('questionpicker.php');

function check_answer($answer, $test) {
	if (!$GLOBALS['config']['case_sensitive']) {
		$answer = strtolower($answer);
		$test = strtolower($test);
		}
	if (!$GLOBALS['config']['pural_sensitive']) {
		$answer = eregi_replace('s$', '', $answer);
		$test = eregi_replace('s$', '', $test);
		}
	return ($answer == $test);
	}

$answer = gi('answer');
$attempts = gi('attempts'); //an 'attempt' is an incorrect answer or receiving a hint

//make sure question zero != no question
$prevquestion = gi('question');
if (!$prevquestion) { $prevquestion = -1; }
else { $prevquestion -= 1; }

//load in existing wrong answers and answer history
$tmp = gi('wrong_answers');
if ($tmp) {
	$tmp = explode('XfooX', $tmp);
	foreach ($tmp as $t) { $wrong_answers[$t] = true; }
	}
$answers_history = explode(',', gi('answers_history'));
if (count($answers_history)==1 && !$answers_history[0]) { $answers_history[0] = 0; } //fix bug with first question correct being recorded as null
//load in answer history summary
//the data is stored with questions delimited by commas and then the bits of data by pipes
$answers_history_sum = gi('answers_history_sum');
if ($answers_history_sum) { $answers_history_sum = explode(',', $answers_history_sum); }
else { //need to initialize
	foreach ($defs as $foo) { $answers_history_sum[] = '0|0|0'; }
	}

//how did they do and what question should be shown
if ($prevquestion != -1 && gi('todo') == $strings['show_answer']) { //reveal the answer
	$thisquestion = $prevquestion;
	$reveal = true;
	$face = 'neutral';
	$answers_history[count($answers_history)-1] += 3; //revealing the answer is "equal" to 3 errors
	}
elseif ($prevquestion != -1 && gi('todo') == $strings['continue']) { //go to next question (having already shown the answer)
	$thisquestion = new_question($prevquestion+1);
	$wrong_answers = false;
	$answers_history[] = 0; //start a new question
	$face = 'neutral';
	}
elseif ($prevquestion != -1 && gi('todo') == $strings['skip']) { //skip the question
	$thisquestion = new_question($prevquestion+1);
	$wrong_answers = false;
	$answers_history[count($answers_history)-1] = -1; //give the skip value
	$answers_history[] = 0; //start a new question
	$face = 'neutral';
	}
elseif (!$answer && $prevquestion == -1) { //no question and no answer
	$thisquestion = new_question();
	$wrong_answers = false;
	}
elseif (!$answer) { //no answer
	$thisquestion = $prevquestion;
	$face = 'neutral';
	}
elseif ($prevquestion>=0 && check_answer($defs[$prevquestion]['term'], $answer)) { // correct answer
	$thisquestion = new_question($prevquestion+1);
	$wrong_answers = false;
	$face = 'right';
	if (count($answers_history)>2 && $answers_history[count($answers_history)-2] == 0 && $answers_history[count($answers_history)-1] == 0) {
		$face = 'tworight';
		if (count($answers_history)>3 && $answers_history[count($answers_history)-3] == 0) {
			$face = 'threeright';
			}
		}
	$answers_history[] = 0;
	}
else { //wrong answer
	$thisquestion = $prevquestion;
	$wrong_answers[$answer] = true;
	$answers_history[count($answers_history)-1]++; //add one wrong answer to the history
	$face = 'wrong' . count($wrong_answers);
	if (count($wrong_answers)>4) {
		$face = 'wrongmore';
		}
	}

echo '<?xml version="1.0" encoding="UTF-8" ?>';
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Quizify Definitions</title>
	<meta http-equiv="Content-Style-Type" content="text/css" />
	<link rel="stylesheet" href="def.css" type="text/css" title="default" media="screen" />
	<link rel="shortcut icon" href="http://faganm.com/quizify/favicon.ico" />
</head>
<body>

<form method="post" action="">
	<div>
		<input type="hidden" name="question" value="<?php echo ($thisquestion + 1); /* make sure question zero != no question */ ?>" />
		<?php if ($wrong_answers) { ?><input type="hidden" name="wrong_answers" value="<?php echo implode('XfooX', array_keys($wrong_answers)); ?>" /><?php } ?>
		<input type="hidden" name="answers_history" value="<?php echo implode(',', $answers_history); ?>" />
		<input type="hidden" name="answers_history_sum" value="<?php echo implode(',', $answers_history_sum); ?>" />
		<input type="hidden" name="definitions" value="<?php echo htmlentities(gi('definitions')); ?>" />
		<input type="hidden" name="useskip" value="<?php echo $config['skip']; ?>" />
		<input type="hidden" name="multiplechoice" value="<?php echo $_POST['multiplechoice']; ?>" />
	</div>

	<div id="question_area">
		<?php if (!$reveal) { ?>
			<?php if (!$config['multiplechoice']) { ?>
				<label><input type="text" name="answer" id="answer" size="15" autocomplete="off" /><script type="text/javascript">document.getElementById('answer').focus();</script></label>
			<?php
				}
			else {
				echo "<div id=\"mc\">";
				foreach ($defs as $foo => $def) {
					echo "<input type=\"submit\" name=\"answer\"";
					if ($wrong_answers[$def['term']]) { echo " class=\"mc_wrong\""; }
					echo " value=\"";
					echo $def['term'];
					echo "\" />";
					}
				echo "</div>";
				}
			}
		?>

		<dl>
			<?php if ($reveal) { ?>
				<dt><?php echo $defs[$thisquestion]['term']; ?></dt>
			<?php } ?>
			<dd><label for="answer"><?php if (!$config['multiplechoice'] || $reveal) { echo ": "; } echo $defs[$thisquestion]['def']; ?></label></dd>
		</dl>
	</div>

	<div id="buttons">
		<?php if ($reveal) { ?>
			<input type="submit" name="todo" value="<?php echo $strings['continue']; ?>" id="continue" class="nonchecksubmit" />
		<?php } else { ?>
			<?php if (!$config['multiplechoice']) { ?><input type="submit" name="todo" value="<?php echo $strings['check_answer']; ?>" id="check_answer" /><?php } ?>
			<input type="submit" name="todo" value="<?php echo $strings['show_answer']; ?>" id="show" class="nonchecksubmit" />
			<?php if ($config['skip']) { ?><input type="submit" name="todo" value="<?php echo $strings['skip']; ?>" id="skip" class="nonchecksubmit" /><?php } ?>
		<?php } ?>
	</div>

	<?php //show wrong answers given so far for this question
	if ($wrong_answers && !$config['multiplechoice']) {
		echo '<div id="wrong_answers"><h2>' . $strings['wrong_answers'] . '</h2><ol>';
		foreach ($wrong_answers as $ans => $foo) {
			echo "<li>$ans</li>";
			}
		echo '</ol></div>';
		}
	?>

	<?php // show answers history
	// answers: -1 = skipped question; # = number of incorrect answers or hints needed to get the correct answer
	// the outer if() and first inner if() are designed to not show the current, not-yet-answers question in the history
	if (count($answers_history)>1 || $answers_history[count($answers_history)-1] != 0) {
		$completed_answers = $answers_history;
		$completed = count($completed_answers) - 1;
		if ($answers_history[count($answers_history)-1] == 0) {
			$completed_answers = array_reverse(array_splice(array_reverse($answers_history), 1)); //don't show just-started question
			}
		/* figure out what the history overview will say */
		$history_overview  = $strings['history_overview_plural'];
		if ($completed == 1) { $history_overview = $strings['history_overview_singular']; }
		$history_overview = str_replace('NUM_QUESTIONS', $completed, $history_overview);
		$firsttry = 0;
		foreach ($completed_answers as $ans) { if ($ans == 0) { $firsttry++; } }
		$history_overview = str_replace('NUM_FIRSTTRY', $firsttry, $history_overview);
		/* output the history */
		echo '<div id="answers_history" title="' . $history_overview . '"><h2 class="' . $face . '">' . $strings['history'] . '</h2><ol>';
		foreach ($completed_answers as $ans) {
			$type = 'right';
			if ($ans == -1) { $type = 'skip'; $ans = 'skipped'; }
			$class = $ans;
			if ($ans > 9) { $class = 'max'; }
			if ($ans > 0) { $type = 'wrong'; }
			if ($ans == 1) { $ans .= ' try'; }
			elseif ($ans != 'skipped') { $ans .= ' tries'; }
			echo "<li class=\"history_$type history_n$class\">&nbsp;<span>$ans</span></li>"; //the non-breaking space is a hack for IE
			}
		echo '</ol></div>';
		}
	?>

</form>

</body>
</html>